#!/usr/bin/env python3
"""
儿童肺炎治疗咨询专家智能体启动脚本
"""

import os
import sys
import subprocess

def install_requirements():
    """安装依赖包"""
    print("正在安装依赖包...")
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
        print("✅ 依赖包安装完成")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ 安装依赖包失败: {e}")
        return False

def check_env():
    """检查环境变量"""
    if not os.path.exists(".env"):
        print("⚠️  未找到.env文件，创建中...")
        with open(".env", "w") as f:
            f.write("KIMI_API_KEY=your_kimi_api_key_here\n")
            f.write("KIMI_BASE_URL=https://api.moonshot.cn/v1")
        print("📋 已创建.env文件，请配置KIMI_API_KEY")
        return False
    
    with open(".env", "r") as f:
        content = f.read()
        if "your_kimi_api_key_here" in content:
            print("⚠️  请先在.env文件中设置您的KIMI_API_KEY")
            return False
    
    return True

def main():
    """主函数"""
    print("🚀 启动儿童肺炎治疗咨询专家智能体...")
    
    # 检查并安装依赖
    if not install_requirements():
        return
    
    # 检查环境变量
    if not check_env():
        return
    
    # 启动Streamlit应用
    print("🎯 正在启动应用...")
    try:
        subprocess.run([sys.executable, "-m", "streamlit", "run", "medical_agent.py"])
    except KeyboardInterrupt:
        print("\n👋 应用已停止")
    except Exception as e:
        print(f"❌ 启动应用失败: {e}")

if __name__ == "__main__":
    main()